package top.lilele.adminSystem.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * 异步线程池
 *
 * @author lilele
 */
@Configuration
public class AsyncConfig {
    /**
     * 最大线程数
     */
    private static final int MAX_POOL_SIZE = 100;

    /**
     * 核心线程数
     */
    private static final int CORE_POOL_SIZE = 50;

    /**
     * 最大队列个数
     */
    private static final int MAX_QUEUE_CAPACITY = 1000;

    /**
     * 检测线程池内线程存活的时长
     */
    private static final int MAX_KEEP_ALIVE_SECONDS = 20;

    @Bean("asyncTaskExecutor")
    public AsyncTaskExecutor asyncTaskExecutor() {
        ThreadPoolTaskExecutor asyncTaskExecutor = new ThreadPoolTaskExecutor();
        asyncTaskExecutor.setMaxPoolSize(MAX_POOL_SIZE);
        asyncTaskExecutor.setCorePoolSize(CORE_POOL_SIZE);
        asyncTaskExecutor.setQueueCapacity(MAX_QUEUE_CAPACITY);
        asyncTaskExecutor.setKeepAliveSeconds(MAX_KEEP_ALIVE_SECONDS);
        asyncTaskExecutor.initialize();
        return asyncTaskExecutor;
    }
}
